/*
 * @lc app=leetcode id=13 lang=javascript
 *
 * [13] Roman to Integer
 *
 * https://leetcode.com/problems/roman-to-integer/description/
 *
 * algorithms
 * Easy (52.03%)
 * Total Accepted:    395.1K
 * Total Submissions: 759.5K
 * Testcase Example:  '"III"'
 *
 * Roman numerals are represented by seven different symbols: I, V, X, L, C, D
 * and M.
 *
 *
 * Symbol       Value
 * I             1
 * V             5
 * X             10
 * L             50
 * C             100
 * D             500
 * M             1000
 *
 * For example, two is written as II in Roman numeral, just two one's added
 * together. Twelve is written as, XII, which is simply X + II. The number
 * twenty seven is written as XXVII, which is XX + V + II.
 *
 * Roman numerals are usually written largest to smallest from left to right.
 * However, the numeral for four is not IIII. Instead, the number four is
 * written as IV. Because the one is before the five we subtract it making
 * four. The same principle applies to the number nine, which is written as IX.
 * There are six instances where subtraction is used:
 *
 *
 * I can be placed before V (5) and X (10) to make 4 and 9.
 * X can be placed before L (50) and C (100) to make 40 and 90.
 * C can be placed before D (500) and M (1000) to make 400 and 900.
 *
 *
 * Given a roman numeral, convert it to an integer. Input is guaranteed to be
 * within the range from 1 to 3999.
 *
 * Example 1:
 *
 *
 * Input: "III"
 * Output: 3
 *
 * Example 2:
 *
 *
 * Input: "IV"
 * Output: 4
 *
 * Example 3:
 *
 *
 * Input: "IX"
 * Output: 9
 *
 * Example 4:
 *
 *
 * Input: "LVIII"
 * Output: 58
 * Explanation: L = 50, V= 5, III = 3.
 *
 *
 * Example 5:
 *
 *
 * Input: "MCMXCIV"
 * Output: 1994
 * Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
 *
 */
/**
 * @param {string} s
 *
 */
// 解法一
// var romanToInt = function(s) {
//     const roman = {
//         I : 1,
//         V : 5,
//         X : 10,
//         L : 50,
//         C : 100,
//         D : 500,
//         M : 1000,
//     }
//     const ar = [];
//     const strarr=s.split("");
//     let num=0;
//     for (let i=0,sl=s.length;i<sl;i+=1){
//         if(strarr[i]=="I"&&(strarr[i+1]=="V"||strarr[i+1]=="X")){
//             ar.push(-(roman[strarr[i]]));
//             continue;
//         }
//         if(strarr[i]=="X"&&(strarr[i+1]=="L"||strarr[i+1]=="C")){
//             ar.push(-(roman[strarr[i]]));
//             continue;
//         }
//         if(strarr[i]=="C"&&(strarr[i+1]=="D"||strarr[i+1]=="M")){
//             ar.push(-(roman[strarr[i]]));
//             continue;
//         }
//         ar.push(roman[strarr[i]]);
//     }
//     for(let i=0,arl=ar.length;i<arl;i+=1){
//         num+=ar[i];
//     }
//     return num;
// };

//解法二
// var romanToInt = function(s) {
//   let match = s.match(/(M*)(C*D*C*M*)(X*L*X*C*)(I*V*I*X*)/).splice(1, 4)
//   // let arr1 = ["","M","MM","MMM"];
//   let map = {
//     '': 0,
//     C: 1,
//     CC: 2,
//     CCC: 3,
//     CD: 4,
//     D: 5,
//     DC: 6,
//     DCC: 7,
//     DCCC: 8,
//     CM: 9,
//     X: 1,
//     XX: 2,
//     XXX: 3,
//     XL: 4,
//     L: 5,
//     LX: 6,
//     LXX: 7,
//     LXXX: 8,
//     XC: 9,
//     I: 1,
//     II: 2,
//     III: 3,
//     IV: 4,
//     V: 5,
//     VI: 6,
//     VII: 7,
//     VIII: 8,
//     IX: 9
//   }
//   return +('' + match[0].length + map[match[1]] + map[match[2]] + map[match[3]])
// }

// 解法三 这个解法有点迷  先放着
var romanToInt = function(s) {
  let obj = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 }
  let num = obj[s[s.length - 1]]
//   console.log(obj[s[2]])
  let a
  let b
  for (a = s.length - 1; a > 0; a = --b) {
    for (b = a; b > 0; b--) {
      if (obj[s[b]] > obj[s[b - 1]]) {
        num -= obj[s[b - 1]]
      } else {
        num += obj[s[b - 1]]
        break
      }
    }
  }
  return num
}
// console.log(romanToInt('III'))
